package com.example.administrator.waitan.homepage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IntegerRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.waitan.R;
import com.example.administrator.waitan.homepage.homepagercontent.HomePagerContentActivity;
import com.example.administrator.waitan.tools.GsonRequest;
import com.example.administrator.waitan.tools.VolleySingleton;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/1/11.
 */
public class HomePagerFragment extends Fragment {
    private Context context;
    private ListView mListView;
    private HomePagerBaseAdapter mAdapter;
    private String url = "http://a.17173.com/api/photos/list?sections=5&unread=false&uuid=000000000000000&pageSize=10&pageIndex=0&maxPublicDate=0&platCode=ANDROID";
    private RequestQueue queue;
    private HomePagerBeans beans;
    private ImageLoader imageLoader;
    private HomePagerViewPagerAdapter homeVPAdapter;
    private ViewPager mViewPager;
    private ArrayList<View> views;
    private List<Integer> Datas;
    private HashMap<Integer, Integer> hashMap;
    private int currenttab = 0;
    private int viewPagerItem = 0;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            mViewPager.setCurrentItem(viewPagerItem % 3);
            viewPagerItem++;
            return false;
        }
    });

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_homepager, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mListView = (ListView) getActivity().findViewById(R.id.lv_homepager);
        homeVPAdapter = new HomePagerViewPagerAdapter(getActivity());
        View headView = LayoutInflater.from(getActivity()).inflate(R.layout.item_lv_homepager_head, null);
        mViewPager = (ViewPager) headView.findViewById(R.id.item_vp_homePager);
        mListView.addHeaderView(headView);
        mViewPager.setAdapter(homeVPAdapter);
        mAdapter = new HomePagerBaseAdapter(getActivity());
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(4000);
                        handler.sendEmptyMessage(0);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final HomePagerProgressDialog dialog = new HomePagerProgressDialog(context, "页面加载中......", R.anim.activity_in);
        dialog.show();
        GsonRequest<HomePagerBeans> homePagerBeansGsonRequest = new GsonRequest<HomePagerBeans>(url, HomePagerBeans.class, new Response.Listener<HomePagerBeans>() {
            @Override
            public void onResponse(final HomePagerBeans response) {
                mAdapter = new HomePagerBaseAdapter(getActivity());
                mAdapter.addData(response.getData().getList());
                HomePagerHelper.getHomePagerHelper().addAll(response.getData().getList());
                mListView.setAdapter(mAdapter);
                dialog.dismiss();
                mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent intent = new Intent(getActivity(), HomePagerContentActivity.class);
                        intent.putExtra("title", response.getData().getList().get(i - 1).getTitle());
                        intent.putExtra("ID", response.getData().getList().get(i - 1).getID());
                        startActivityForResult(intent, 200);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                    mAdapter.addData(HomePagerHelper.getHomePagerHelper().queryAll());
                    mListView.setAdapter(mAdapter);
                dialog.dismiss();
            }
        });
        VolleySingleton.getInstance()._addRequest(homePagerBeansGsonRequest);
    }

}
